package de.bib.blue.manage.finance.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import de.bib.blue.manage.finance.util.UIHelper;

public class SearchDialog extends TitleAreaDialog {

	private static final String MESSAGE = "Please specify parameter";

	private String[] FIELDS = { "Tgl_Transaksi",  "Kode_Transaksi", "Jumlah_Transaksi" };
	public String[] OPERANDS = { "=", ">", "<", "<>", "<=", ">=", "like" };

	private final String title;

	protected Button ok = null;
	private CCombo ccFields = null;
	private CCombo ccOperands = null;
	private Text valueText = null;

	private String fields = null;
	private String operands = null;
	private String value = null;

	public SearchDialog(Shell parentShell) {
		super(parentShell);
		title = "Seacrh Form";
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle(title);
		setMessage(MESSAGE);

		/*
		 * Button ok di-hidden agar user tidak salah login.
		 */
		ok = getButton(IDialogConstants.OK_ID);
		ok.setEnabled(false);

		return contents;

	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(UIHelper.fillBoth());
		composite.setLayout(new GridLayout(3, false));

		ccFields = new CCombo(composite, SWT.BORDER | SWT.READ_ONLY);
		ccFields.setLayoutData(UIHelper.fillHorizontal());
		ccFields.setItems(FIELDS);
		ccFields.select(0);
		ccFields.setEditable(false);

		ccOperands = new CCombo(composite, SWT.BORDER | SWT.READ_ONLY);
		ccOperands.setLayoutData(UIHelper.fillHorizontal());
		ccOperands.setItems(OPERANDS);
		ccOperands.select(0);
		ccOperands.setEditable(false);

		valueText = UIHelper.createText(composite);
		valueText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				checkCompleted();
			}
		});

		return composite;
	}

	protected void checkCompleted() {
		setErrorMessage(null);
		setMessage(MESSAGE);
		if (valueText != null) {
			if (valueText.getText().length() > 0) {
				ok.setEnabled(true);

				fields = ccFields.getText();
				operands = ccOperands.getText();
				value = valueText.getText();
			}
		}

	}

	public String getFields() {
		return fields;
	}

	public void setFields(String fields) {
		this.fields = fields;
	}

	public String getOperands() {
		return operands;
	}

	public void setOperands(String operands) {
		this.operands = operands;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

}
